package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.execption.CheckItemExecption;
import com.itheima.pojo.CheckItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {


    @Autowired
    CheckItemDao checkItemDao;

    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //告诉mybatis我要分页了
        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());

        List<CheckItem> checkItems = checkItemDao.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),checkItems);
    }

    @Override
    public void delete(Integer id) throws CheckItemExecption{
        //判断当前检查项是否存在引用
        Integer count = checkItemDao.findCheckItemCountByCheckItemId(id);
        if(count > 0){
            //提示用户
            throw new CheckItemExecption("存在引用，不能删除");
        } else {
            checkItemDao.delete(id);
        }

    }

    @Override
    public CheckItem findById(Integer id) {
        return checkItemDao.findById(id);
    }

    @Override
    public void update(CheckItem checkItem) {
        checkItemDao.update(checkItem);
    }

    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }
}
